package se.webbzon.boltzmann.editor.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.boltzmann.editor.mvc.Grid3D;

public class GridFrame {
	
	// The frame
	private final JInternalFrame frame;
	
	// The input fields for this frame
	private final JTextField xInput, yInput, zInput;
	
	// The 3-dimensional grid associated with this frame
	private final Grid3D grid;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public GridFrame(EditorModel model) {
		grid = model.getGrid();
		frame = new JInternalFrame("Set grid:");
		
		frame.setClosable(true);
		frame.setLayout(new GridLayout(4,2));
		
		xInput = new JTextField("" + grid.getWidth());
		yInput = new JTextField("" + grid.getHeight());
		zInput = new JTextField("" + grid.getDepth());
		frame.add(new JLabel("Grid width:"));
		frame.add(xInput);
		frame.add(new JLabel("Grid height:"));
		frame.add(yInput);
		frame.add(new JLabel("Grid depth:"));
		frame.add(zInput);
		frame.add(new JButton(new CancelAction()));
		frame.add(new JButton(new OKAction()));
		
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		model.getDesktopPane().add(frame);
		frame.toFront();
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	/** An implementation of an abstract action used for the Cancel button. **/
	private final class CancelAction extends AbstractAction {
		
		// Auto generated serial version UID
		private static final long serialVersionUID = -3666826611824016525L;
		
		/*============================================================
		Constructors
		============================================================*/
	
		public CancelAction() {
			putValue(AbstractAction.NAME,"Cancel");
		}
		
		/*============================================================
		Public Methods
		============================================================*/

		@Override public void actionPerformed(ActionEvent event) {
			frame.dispose();
		}
		
	}
	
	/** An implementation of an abstract action used for the Cancel button. **/
	private final class OKAction extends AbstractAction {
		
		// Auto generated serial version UID
		private static final long serialVersionUID = -8569614104324354533L;
		
		/*============================================================
		Constructors
		============================================================*/

		public OKAction() {
			putValue(AbstractAction.NAME,"OK");
		}
		
		/*============================================================
		Public Methods
		============================================================*/

		@Override public void actionPerformed(ActionEvent event) {
			try {
				grid.setWidth(Double.parseDouble(xInput.getText()));
				grid.setHeight(Double.parseDouble(yInput.getText()));
				grid.setDepth(Double.parseDouble(zInput.getText()));
				frame.dispose();
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(	frame, e.getMessage(), 
												"Error", 
												JOptionPane.ERROR_MESSAGE);
			}
			
		}
		
	}

}
